﻿namespace PugLib
{
	using System;
	using System.Globalization;
	using System.Threading;

	/// <summary>
	/// Simple wrapper for incrementing and decrementing a counter
	/// </summary>
	public sealed class ThreadSafeCounter
	{
		private Int32 _counter;

		/// <summary>
		/// True if the count is 0, false otherwise
		/// </summary>
		public Boolean IsEmpty
		{
			get { return (0 == Value); }
		}

		/// <summary>
		/// The current value of the counter.
		/// </summary>
		public Int32 Value
		{
			get { return Thread.VolatileRead(ref _counter); }
		}

		/// <summary>
		/// Adds one to the count
		/// </summary>
		public void Increment()
		{
			Interlocked.Increment(ref _counter);
		}

		/// <summary>
		/// Subtracts one from the count
		/// </summary>
		public void Decrement()
		{
			Interlocked.Decrement(ref _counter);
		}

		/// <summary>
		/// Volatile read value
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return String.Format(CultureInfo.CurrentCulture, "Value: {0}", Value);
		}
	}
}